#include "qcomboboxhost.h"

#include "../property/qabstractproperty.h"
#include "../qpropertyfactory.h"
#include "../qcommonstruct.h"
#include "../qhostfactory.h"

#include <QComboBox>

QComboBoxHost::QComboBoxHost(QAbstractHost *parent):
    QWidgetHost(parent)
{
    setProperty("need_frame",false);
    setProperty("accept_drop",false);

    setProperty("edit_property","comboItem");
}

QString QComboBoxHost::get_show_name()
{
    return tr("Combo Box");
}

QString QComboBoxHost::get_show_group()
{
    return tr("Input Widgets");
}

QString QComboBoxHost::get_show_icon()
{
    return ":/images/combobox.png";
}

void QComboBoxHost::create_object()
{
    m_object=new QComboBox();
    m_object->setObjectName("combobox");
}

void QComboBoxHost::init_property()
{
    QWidgetHost::init_property();

    QAbstractProperty *pro;

    pro=QPropertyFactory::create_property("Number");
    if(pro!=NULL)
    {
        pro->set_property("name","currentIndex");
        pro->set_attribute("show_name",tr("CurrentIndex"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro,1);
    }

    pro=QPropertyFactory::create_property("ComboItem");
    if(pro!=NULL)
    {
        pro->set_property("name","comboItem");
        pro->set_attribute("show_name",tr("Items"));
        pro->set_attribute("group","Attributes");
        m_object->setProperty("comboItem",pro->get_value());
        connect(pro,SIGNAL(refresh()),this,SLOT(update_item()));
        insert_property(pro,2);
    }

    pro=QPropertyFactory::create_property("Number");
    if(pro!=NULL)
    {
        pro->set_property("name","maxVisibleItems");
        pro->set_attribute("show_name",tr("MaxVisibleItems"));
        pro->set_attribute("group","Attributes");
        pro->set_attribute(ATTR_CAN_SAME,true);
        insert_property(pro);
    }

    pro=QPropertyFactory::create_property("Script");
    if(pro!=NULL)
    {
        pro->set_property("name","currentIndexChanged");
        pro->set_attribute("show_name",tr("CurrentIndexChanged"));
        pro->set_attribute("group","Events");
        insert_property(pro);
    }

    QComboBox *b=(QComboBox*)m_object;
    connect(b,SIGNAL(currentIndexChanged(int)),this,SLOT(current_index_changed(int)));

    set_property_value("geometry",QRect(0,0,100,20));
}

void QComboBoxHost::current_index_changed(int index)
{
    QString code=get_property_value("currentIndexChanged").toString();
    if(code!="")
    {
        QMap<QString,QVariant> param,ret;
        param.insert("index",index);
        param.insert("text",((QComboBox*)m_object)->currentText());
        exec(code,param,ret);
    }
}

void QComboBoxHost::update_item()
{
    QVariant value=get_property_value("comboItem");

    ComboBoxItems items=value.value<ComboBoxItems>();

    QComboBox *b=(QComboBox*)m_object;

    int current=b->currentIndex();

    b->clear();

    foreach(tagComboBox item,items)
    {
        if(item.m_icon!="")
        {
            b->addItem(QIcon(":/"+item.m_icon),item.m_text);
        }
        else
        {
            b->addItem(item.m_text);
        }
    }

    if(current>=items.size())
    {
        current=items.size()-1;
    }

    b->setCurrentIndex(current);
}

qRegisterHost("inputwidgets.combobox",QComboBoxHost,2070)
